package com.imyours.webservice.services;

import java.util.List;

import org.hibernate.Session;

import com.imyours.model.team.TeamEntity;
import com.imyours.model.user.UserEntity;
import com.imyours.model.util.HibernateUtil;
import com.imyours.webservice.service.Context;
import com.imyours.webservice.service.ContextService;
import com.imyours.webservice.services.annotation.Service;

/**
 * 修改分组顺序和名称
 * 
 * @author SLevenc
 * 
 */
@Service(name = "modifyTeam")
public class ModifyTeam extends ContextService {

	public ModifyTeam(Context context) {
		super(context);
	}

	public void execute() {
		UserEntity user = getUserEntity();
		if (user != null) {
			String teamid = context.getParameter().get("id");
			String teamorder = context.getParameter().get("order");
			String teamName = context.getParameter().get("name");

			if (teamid != null) {
				Session se = HibernateUtil.getSessionFactory().openSession();
				List<TeamEntity> tes = se.createQuery("from TeamEntity where user = ? and id = ?").setEntity(0, user)
						.setLong(1, Long.parseLong(teamid)).list();
				if (tes.size() == 1) {
					TeamEntity teamEntity = tes.get(0);
					teamEntity.setOrder(Integer.parseInt(teamorder));
					teamEntity.setName(teamName);
					se.beginTransaction();
					se.update(teamEntity);
					se.getTransaction().commit();
					se.close();
					context.putResult("id", teamEntity.getId());
					context.putResult("name", teamEntity.getName());
					context.putResult("order", teamEntity.getOrder());
					context.success();
				}
			}
		}
	}

}
